package com.work.biz.controller;

import java.util.List;

import com.work.mp.service.impl.MpTokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.work.common.annotation.Log;
import com.work.common.core.controller.BaseController;
import com.work.common.core.domain.AjaxResult;
import com.work.common.enums.BusinessType;
import com.work.biz.domain.BizCounselor;
import com.work.biz.service.IBizCounselorService;
import com.work.common.utils.poi.ExcelUtil;
import com.work.common.core.page.TableDataInfo;

/**
 * 辅导员Controller
 *
 * @author  lee
 * @date 2021-04-22
 */
@RestController
@RequestMapping("/api/biz/counselor")
@Api(value = "辅导员api", tags = {"添加辅导员接口"})
public class BizCounselorController extends BaseController {
    @Autowired
    private IBizCounselorService ibizCounselorService;

    @Autowired
    private MpTokenService tokenService;

    /**
     * 查询辅导员列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BizCounselor bizCounselor) {
        startPage();
        List<BizCounselor> list = ibizCounselorService.selectBizCounselorList(bizCounselor);
        return getDataTable(list);
    }

    /**
     * 导出辅导员列表
     */
    @Log(title = "辅导员", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BizCounselor bizCounselor) {
        List<BizCounselor> list = ibizCounselorService.selectBizCounselorList(bizCounselor);
        ExcelUtil<BizCounselor> util = new ExcelUtil<BizCounselor>(BizCounselor.class);
        return util.exportExcel(list, "辅导员数据");
    }

    /**
     * 获取辅导员详细信息
     */
    @GetMapping(value = "/{counselorId}")
    public AjaxResult getInfo(@PathVariable("counselorId") Long counselorId) {
        return AjaxResult.success(ibizCounselorService.selectBizCounselorById(counselorId));
    }

    /**
     * 新增辅导员
     */
    @Log(title = "辅导员", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation(value = "指定辅导员", httpMethod = "POST", notes = "成功：0，验证失败：500")
    public AjaxResult add(
            @ApiParam(value = "用户token", required = true)
            @RequestHeader("tk")
                    String tk,
            @ApiParam(value = "辅导员信息", required = true)
            @RequestBody BizCounselor bizCounselor
    ) {
        BizCounselor c = new BizCounselor();
        c.setUserId(bizCounselor.getUserId());
        c.setSchoolId(bizCounselor.getSchoolId());
        List<BizCounselor> list = ibizCounselorService.selectBizCounselorList(c);
        if(list.size()>0) {
            return error("已指定过该用户未该校辅导员");
        }else {
            bizCounselor.setCounselorId(tokenService.getUserId(tk));
            return toAjax(ibizCounselorService.insertBizCounselor(bizCounselor));
        }
    }

    /**
     * 修改辅导员
     */
    @Log(title = "辅导员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizCounselor bizCounselor) {
        return toAjax(ibizCounselorService.updateBizCounselor(bizCounselor));
    }

    /**
     * 删除辅导员
     */
    @Log(title = "辅导员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{counselorIds}")
    public AjaxResult remove(@PathVariable Long[] counselorIds) {
        return toAjax(ibizCounselorService.deleteBizCounselorByIds(counselorIds));
    }
}
